package br.senac.arqpad.pizzaria.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DBUtil 
{
	// Singleton
	private static DBUtil singleton;
	private Connection con;
    private DBUtil(){}
    
	public static DBUtil getInstance()
	{
		if (singleton == null)
			singleton = new DBUtil();
		
		return singleton;
	}	
	public Connection getConnection()
	{
		// Se a conex�o n�o existe ainda

			try
			{
				if (con == null || con.isClosed())
				{
				// Carrega a classe do driver JDBC   
				Class.forName("org.postgresql.Driver");   
		    
				// Cria uma conex�o atrav�s do Driver   
				con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/academicNet","postgres","admin");
				}
			}
			catch(ClassNotFoundException e)
			{
				throw new RuntimeException("O driver nao foi importado",e);
			}
			catch(SQLException e)
			{
				throw new RuntimeException("Erro de conexao com o banco",e);
			}
		
		
		// Retorna a conex�o
		return con;
	}
}